package com.fzdy.dataplatform.dao.vo.impl;


import com.fzdy.dataplatform.core.common.Check;
import com.fzdy.dataplatform.core.dao.impl.BaseDaoImpl;
import com.fzdy.dataplatform.dao.vo.DeviceOwnerVoDao;
import com.fzdy.dataplatform.entity.DeviceOwnerVo;
import org.hibernate.Criteria;
import org.hibernate.criterion.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DeviceOwnerVoImpl extends BaseDaoImpl<DeviceOwnerVo> implements DeviceOwnerVoDao {
    @Override
    public List<DeviceOwnerVo> findByExampleLike(DeviceOwnerVo example, int pageNo, int pageSize) {
        Criteria criteria = this.getSession().createCriteria(this.clazz).add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        if (!Check.checkStringNull(example.getSn()))
            criteria.add(Restrictions.like("sn", example.getSn(), MatchMode.ANYWHERE));
        return this.createPageOrder(criteria, pageNo, pageSize, Order.desc("id")).list();
    }

    @Override
    public long countByExampleLike(DeviceOwnerVo example) {
        Criteria criteria = this.getSession().createCriteria(this.clazz).add(Example.create(example)
                .enableLike(MatchMode.ANYWHERE));
        if (!Check.checkStringNull(example.getSn()))
            criteria.add(Restrictions.like("sn", example.getSn(), MatchMode.ANYWHERE));
        Long o = (Long) criteria.setProjection(Projections.count("id")).uniqueResult();
        return o.longValue();
    }
}
